Canny Edge Detection

Canny function example

edge_image = cv2.Canny(gray_image, low_threshold, high_threshold)

In [11]:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('testimg.jpg')
plt.imshow(img)
plt.show()


Convert to grayscale first


In [12]:
import cv2
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
plt.imshow(gray_img, cmap='gray')
plt.show()



In [24]:
import numpy as np

# Kernel size is optional, default is 5X5 Gaussian
kernel_size = 3
blur_gray = cv2.GaussianBlur(gray_img,(kernel_size, kernel_size), 0)

# play with these threshold vals to get the correct edges
low_threshold = 50
high_threshold = 200 # quiz was 50/150. Not bad!
edges = cv2.Canny(gray_img, low_threshold, high_threshold)
plt.imshow(edges, cmap='Greys_r')
plt.show()



In [ ]: